package io.gbznt.modules.buss.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.R;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.common.validator.ValidatorUtils;
import io.gbznt.common.validator.group.AddGroup;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.entity.BussRtuWarningRoleEntity;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.buss.service.BussRtuWarningRoleService;



/**
 * RTU 告警规则
 *
 * @author gbznt
 * @email http://www.gbznt.com/
 * @date 2019-04-20 15:48:34
 */
@RestController
@RequestMapping("buss/bussrtuwarningrole")
public class BussRtuWarningRoleController {
    @Autowired
    private BussRtuWarningRoleService bussRtuWarningRoleService;
    @Autowired
    private BussFarmlandInfoService binfo;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("buss:bussrtuwarningrole:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bussRtuWarningRoleService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("buss:bussrtuwarningrole:info")
    public R info(@PathVariable("id") String id){
        BussRtuWarningRoleEntity bussRtuWarningRole = bussRtuWarningRoleService.getById(id);

      //田块名称
    	String tk = bussRtuWarningRole.getRelationFarmlandIds();
    	String tkname = "";
    	if(StringUtils.isNotBlank(tk)){
    		List<BussFarmlandInfoEntity> brentity = binfo.list(
					new QueryWrapper<BussFarmlandInfoEntity>().in("id", tk.split(","))
			);
    		for (int i = 0; i < brentity.size(); i++) {
    			if(i == 0){
					tkname = brentity.get(i).getName();
					
				}else{
					tkname = tkname +","+brentity.get(i).getName();
				}
			}
    		
    		bussRtuWarningRole.setRelationFarmlandIdsname(tkname);
    	}
        return R.ok().put("bussRtuWarningRole", bussRtuWarningRole);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("buss:bussrtuwarningrole:save")
    public R save(@RequestBody BussRtuWarningRoleEntity bussRtuWarningRole){
    	ValidatorUtils.validateEntity(bussRtuWarningRole,AddGroup.class);
    
   	 	bussRtuWarningRole.setCreateId(ShiroUtils.getUserId()+"");
   	 	//添加部门 当前用户所在部门
   	 	bussRtuWarningRole.setDeptId(ShiroUtils.getUserEntity().getDeptId());
    	bussRtuWarningRole.setCreateTime(new Date());
        bussRtuWarningRoleService.save(bussRtuWarningRole);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("buss:bussrtuwarningrole:update")
    public R update(@RequestBody BussRtuWarningRoleEntity bussRtuWarningRole){
        ValidatorUtils.validateEntity(bussRtuWarningRole,AddGroup.class);
       
        bussRtuWarningRole.setUpdateId(ShiroUtils.getUserId()+"");
    	bussRtuWarningRole.setUpdateTime(new Date());
        Boolean re = bussRtuWarningRoleService.updateById(bussRtuWarningRole);//全部更新
        
        return re?R.ok():R.oldVersion();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("buss:bussrtuwarningrole:delete")
    public R delete(@RequestBody String[] ids){
        bussRtuWarningRoleService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 获取字典
     */
    /*@RequestMapping("/geidic")
    @RequiresPermissions("buss:bussrtuwarningrole:geidic")
    public R geidic(@RequestBody String type){
        bussRtuWarningRoleService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }*/

}
